package com.fxd.crm.web.controller.login;

import com.fxd.crm.model.curdResult;
import com.fxd.crm.model.student;
import com.fxd.crm.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @auther fuxudong
 * @date 2020/12/23 - 14:45
 */
@Controller
public class indexController {
    @Autowired
    IStudentService studentService;

    @RequestMapping("/login")
    public String login(HttpServletRequest request) {
        return "login";
    }

    @RequestMapping("/register")
    public String register() {
        return "register";
    }


    @RequestMapping("/studentIndex")
    public String managerIndex() {
        return "studentIndex";
    }

    @RequestMapping("/manageIndex")
    public String studentIndex() {
        return "index";
    }

    @RequestMapping("/toregister")
    public String save(String name, String password) {
        student student = new student();
        student.setName(name);
        student.setPassword(password);
        studentService.save(student);
        return "login";
    }

    @RequestMapping("/tologin")
    public String tologin(HttpServletRequest request, String identy, String name, String password) {
        HttpSession session = request.getSession();
        session.setAttribute("name", name);
        session.setAttribute("password", password);
        if (identy != null && identy.equals("管理员")) {
            if (name != null && name.equals("manage") && password != null && password.equals("manage")) {
                return "index";
            } else {
                request.setAttribute("msg", "请输入正确的账号密码!");
                return "login";
            }
        } else if (identy != null && identy.equals("学员")) {
            if (name != null && !name.equals("") && password != null && !password.equals("")) {
                student stu = studentService.findByName(name);
                if (stu != null) {
                    if (name.equals(stu.getName()) && password.equals(stu.getPassword())) {
                        return "studentIndex";
                    } else {
                        request.setAttribute("msg", "请输入正确的用户名和密码!");
                        return "login";
                    }
                } else {
                    request.setAttribute("msg", "请输入正确的用户名!");
                    return "login";
                }
            }
        } else {
            request.setAttribute("msg", "请输入你的身份!");
            return "login";
        }
        request.setAttribute("msg", "用户名和密码不能为空");
        return "login";
    }


}
